<?php
/*
 * @Descripttion:
 * @version:
 * @Author: ewgof
 * @Date: 2020-08-31 23:19:09
 * @LastEditors: ewgof
 * @LastEditTime: 2020-09-24 20:57:04
 */
/*
 * @Descripttion:
 * @version:
 * @Author: ewgof
 * @Date: 2020-08-12 22:23:27
 * @LastEditors: ewgof
 * @LastEditTime: 2020-09-01 10:04:13
 */
namespace app\http\controller\api;


class Index extends Base
{
    public function index($request)
    {
        echo 'This is PicBed api module index' . PHP_EOL;
        return 'This is PicBed api module index';
    }


    public function userImages($request)
    {
        $token = $request->header('token');
        if (empty($token)) {
            return api_json_return(10001);
        }
        $rst = check_token($token);
        if ($rst !== true) {
            return $rst;
        }

        $page =  $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $offset = ($page - 1) * $limit;

        $all = $this->db->count('user_imgs', [ '[>]users' => ['uid' => 'id'], ], 'user_imgs.id', ['users.token' => $token]);
        $list = $this->db->select('user_imgs', [ '[>]users' => ['uid' => 'id'], ], ['user_imgs.id', 'user_imgs.uid', 'user_imgs.url', 'user_imgs.mime', 'user_imgs.created_at', 'filename'], ['users.token' => $token, 'ORDER' => ['user_imgs.created_at' => 'DESC'], 'LIMIT' => [$offset, $offset + $limit]]);
        // var_dump($this->db->last());
        if (empty($list)) {
            return api_json_return(4);
        }
        foreach ($list as $lk => $lv) {
            $list[$lk]['date'] = date('Y-m-d H:i:s', $lv['created_at']);
            $img_path = ltrim(strstr($lv['url'], '/upload'), '/');
            $list[$lk]['size'] = humansize(filesize(__ROOT__ . $img_path), true, true, true);
        }
        return api_json_return(0, '', ['all' => $all, 'list' => $list]);
    }



    public function guestImages($request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 20);
        $offset = ($page - 1) * $limit;

        $all = $this->db->count('guest_imgs', 'id');
        $list = $this->db->select('guest_imgs', ['id', 'url', 'mime', 'created_at', 'filename'], ['ORDER' => ['created_at' => 'DESC'], 'LIMIT' => [$offset, $offset + $limit]]);
        if (empty($list)) {
            return api_json_return(4);
        }
        foreach ($list as $lk => $lv) {
            $list[$lk]['date'] = date('Y-m-d H:i:s', $lv['created_at']);
            $img_path = ltrim(strstr($lv['url'], '/upload'), '/');
            $list[$lk]['size'] = humansize(filesize(__ROOT__ . $img_path), true, true, true);
        }
        return api_json_return(0, '', ['all' => $all, 'list' => $list]);
    }



    public function imgDetail($request)
    {
        $id = $request->get('id');
        $uid = $request->get('uid', 0);
        if (empty($id)) {
            return api_json_return(2);
        }
        if (empty($uid)) {
            // 游客公开图片
            $info = $this->db->get('guest_imgs', ['id', 'url', 'filename', 'mime', 'views', 'created_at', 'uid'], ['id' => $id]);
        } else {
            // 用户上传图片
            $info = $this->db->get('user_imgs', ['id', 'url', 'filename', 'mime', 'views', 'created_at'], ['id' => $id]);
        }
        if (empty($info)) {
            return api_json_return(4);
        }
        return api_json_return(0, '', $info);
    }



    public function deleteImg($request)
    {
        $token = $request->header('token');
        $id = $request->get('id');
        $uid = $request->get('uid');
        if (empty($token) || empty($id) || empty($uid)) {
            return api_json_return(2);
        }
        $rst = check_token($token);
        if ($rst !== true) {
            return $rst;
        }

        $url = $this->db->get('user_imgs', 'url', ['id' => $id]);
        $file_path = __ROOT__ . ltrim(strstr($url, '/upload'), '/');

        $res = $this->db->delete('user_imgs', ['id' => $id])->rowCount();
        if ($res === 1) {
            @unlink($file_path);
            return api_json_return(0);
        } else {
            return api_json_return(1);
        }
    }


    public function addViews($request)
    {
        $id = $request->get('id');
        $auth = $request->get('auth');
        if (empty($id) || empty($auth)) {
            return api_json_return(2);
        }
        if ($auth == 'user') {
            $table = 'user_imgs';
        } elseif ($auth == 'guest') {
            $table = 'guest_imgs';
        }
        $views = $this->db->get($table, 'views', ['id' => $id]);
        $views += 1;
        $info = $this->db->update($table, ['views' => $views], ['id' => $id])->rowCount();
        if ($info === 1) {
            return api_json_return(0);
        } else {
            $msg = '更新查看次数失败。id => ' . $id . ', auth => ' . $auth . ', views => ' . $views . ', info => ' . $info;
            echo $msg . PHP_EOL;
            log_msg($msg, 'error', $this->log_filename_apperr);
            return api_json_return(1);
        }
    }
}
